// stores/counter.js
import { defineStore } from 'pinia'
interface Item {
    [key: string]: any
}
export const useCounterStore = defineStore('counter', {
    state: () => {
        return {
            cart:JSON.parse(localStorage.getItem('cart')) || [] as Item[],
            allCheckeds: false,
            allPrices: 0
        }
    },
    // 也可以这样定义
    actions: {
        // 添加购物车
        addCart(item: any) {
            // console.log(item);
            const arr = this.cart.find((i: Item) => i.shop_spu_id === item.shop_spu_id) || ''

            if (arr.length == 0) {
                item.count = 1
                item.checked = false
                this.cart.push(item)
            } else {
                arr.count++
            }
            console.log(arr);
            console.log(this.cart);
            localStorage.setItem('cart', JSON.stringify(this.cart))
        },
        // 删除购物车
        deleteCart(indexs: number) {
            const data = JSON.parse(localStorage.getItem('cart') || '')
            data.forEach((item: Item, index: number) => {
                if (item.shop_spu_id === indexs) {
                    data.splice(index, 1)
                }
            })
            localStorage.setItem('cart', JSON.stringify(data))
        },
        // 单选
        Dchecked(index: number) {
            const data = JSON.parse(localStorage.getItem('cart') || '')
            const item = data.find((i: Item) => i.shop_spu_id === index);
            console.log(item, '1111');
            if (item) {
                item.checked = !item.checked;
            }

            localStorage.setItem('cart', JSON.stringify(data))
        },
        // 全选
        allChecked() {
            this.allCheckeds = !this.allCheckeds
            const data = JSON.parse(localStorage.getItem('cart') || '')

            data.forEach((item: Item) => {
                item.checked = this.allCheckeds
            })

            localStorage.setItem('allCheckeds', JSON.stringify(this.allCheckeds))
            localStorage.setItem('cart', JSON.stringify(data))
        },
        // 总价
        allPrice() {
            const data = JSON.parse(localStorage.getItem('cart') || '')
            let price = 0
            console.log(data, '333');
            data.forEach((item: Item) => {
                if (item.checked) {
                    price += item.goods_info.price * item.count * 100
                }
            })
            this.allPrices = price
        },
        checkAllSelectedStatus() {
            const data = JSON.parse(localStorage.getItem('cart') || '')
            const allSelected = data.every((item: Item) => item.checked == true);
            console.log(allSelected, 'allSelected');
            this.allCheckeds = allSelected;
        },
        // 数量加
        addCount(index: number) {
            const data = JSON.parse(localStorage.getItem('cart') || '')
            const item = data.find((i: Item) => i.shop_spu_id === index);
            if (item) {
                item.count++
            }
            localStorage.setItem('cart', JSON.stringify(data))
        },
        // 数量减
        subCount(index: number) {
            const data = JSON.parse(localStorage.getItem('cart') || '')
            const item = data.find((i: Item) => i.shop_spu_id === index);
            if (item) {
                item.count--
                if (item.count == 0) {
                    return
                }
            }
            localStorage.setItem('cart', JSON.stringify(data))
        }
    },
})